// pages/vote/Creatvote/CreatVote.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    show:false,
    topic:'',
    deadline:'',
    username:'',
    content:'',
    number:'',
    pid:'',
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({checked:detail.value});
  },
  //打开弹窗
  Checktime(){
    this.setData({show:true});
  },
  //提交
  onSubmit(){
    var that=this
    wx.request({
      url: 'http://8.129.104.110/vote', //新增投票接口
      data: {
        voteEntity: {
          completed: false,
          deadline: this.deadline,
          deleted: false,
          multiple: this.checked,
          propertyId: this.pid,
          requiredNumber: this.number,
          topic: this.topic,
          voteContent: this.content,
          votedNumber: 0
        },
        voteOptionsEntities: []
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method:'POST',
      success (res) {
        console.log(res.data)
        Toast('新建成功！');
        that.setData({
          topic:'',
          deadline:'',
          username:'',
          content:'',
          number:'',
        })
      }
    })
  },
  //时间取消按钮
  cancel(){
    this.setData({ show: false });
  },
  //时间确定按钮
  confirm(){
    this.setData({
       show: false ,
       deadline:this.currentDate
    });
  },
  //关闭弹窗
  onClose() {
    this.setData({ show: false });
  },
  //选择时间
  onInput(event) {
    this.setData({
      currentDate: event.detail,
      deadline: this.js_date_time(event.detail)
    });
  },
  //时间戳转换为时间
  js_date_time(unixtime) {
    var date = new Date(unixtime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '年' + m + '月' + d + '日';
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})